package spe.ui.editor.part;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.AbsoluteBendpoint;
import org.eclipse.draw2d.BendpointConnectionRouter;
import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;

import spe.model.notation.Link;
import spe.ui.editor.figure.CirclePlusDecoration;
import spe.ui.editor.policy.LinkBendpointEditPolicy;
import spe.ui.editor.policy.SPELinkEditPolicy;

public class LinkEditPart extends AbstractConnectionEditPart implements PropertyChangeListener
{
	public LinkEditPart()
	{
		super();
	}
	
	public LinkEditPart(Link link)
	{
		setModel(link);
	}
	
	@Override
	protected IFigure createFigure()
	{
		PolylineConnection conn = new PolylineConnection();
		conn.setConnectionRouter(new BendpointConnectionRouter());
		Link link = (Link)getModel();
		
		switch(link.getConnectionType())
		{
			case Link.NOTSPECIFIED:
				break;
				
			case Link.EXTENDED:
				break;
				
			case Link.BELONG_TO:
				CirclePlusDecoration decoration = new CirclePlusDecoration();
				conn.setSourceDecoration(decoration);
				break;
				
			case Link.TRACE:
				break;
				
			case Link.PERFORM:
				conn.setLineStyle(Graphics.LINE_DASH);
				conn.setTargetDecoration(new PolylineDecoration());
				break;
				
			default:			
		}
		
		return conn;
	}

	@Override
	protected void createEditPolicies() 
	{
		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE, new ConnectionEndpointEditPolicy());
		installEditPolicy(EditPolicy.CONNECTION_ROLE, new SPELinkEditPolicy());
		installEditPolicy(EditPolicy.CONNECTION_BENDPOINTS_ROLE, new LinkBendpointEditPolicy());
	}
	
	@Override
	protected void refreshVisuals()
	{
		Connection conn = getConnectionFigure();
		List<Point> modelConstraint = ((Link)getModel()).getBendPoints();
		List<AbsoluteBendpoint> figureConstraint = new ArrayList<AbsoluteBendpoint>();
		
		for(Point p: modelConstraint)
			figureConstraint.add(new AbsoluteBendpoint(p));
		
		conn.setRoutingConstraint(figureConstraint);
	}
	
	@Override
	public void performRequest(Request req)
	{
		//if user double click on icon
		if(req.getType() == RequestConstants.REQ_OPEN)
		{
			
		}
	}
	
	@Override
	public void activate()
	{
		if(!isActive())
			((Link)getModel()).addPropertyChangeListener(this);
		
		super.activate();
	}
	
	@Override
	public void deactivate()
	{
		if(isActive())
			((Link)getModel()).removePropertyChangeListener(this);
		
		super.deactivate();
	}

	@Override
	public void propertyChange(PropertyChangeEvent notification)
	{
		refreshVisuals();
	}

}
